package com.sky.controller.admin;

import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

@Api(tags = "店铺相关接口")
@Slf4j
@RequestMapping("/admin/shop")
@RestController("adminShopController")  //给bean对象起别名,否则类名重复启动报错
public class ShopController {

    @Autowired
    private RedisTemplate redisTemplate;

    private final static String SHOP_STATUS = "SHOP_STATUS";

    /**
     * 店铺营业状态设置--redis存状态
     * @param status  0-打烊， 1-营业
     * @return
     */
    @ApiOperation("店铺营业状态设置")
    @PutMapping("/{status}")
    public Result setStatus(@PathVariable Integer status){
        log.info("店铺状态设置：{}", status);

        // 1.通过RedisTemplate对象获取 valueOperations 对象
        ValueOperations valueOperations = redisTemplate.opsForValue();

        // 2.调用 valueOperations 的方法，操作redis，往redis中存入状态
        valueOperations.set(SHOP_STATUS, status);

        return Result.success();
    }

    @ApiOperation("获取店铺营业状态")
    @GetMapping("/status")
    public Result getStatus(){
        // 1.从redis中获取状态，key是SHOP_STATUS
        Integer status = (Integer) redisTemplate.opsForValue().get(SHOP_STATUS);
        log.info("获取店铺营业状态: {}", status);

        // 2.返回状态
        return Result.success(status);
    }

}
